package controller

import (
	"hft/Database/admin"
	services "hft/Http/Services"
	utils "hft/Http/Utils"
	"hft/tools"
	"strconv"

	"github.com/gin-gonic/gin"
)

type Activity struct{}

func (*Activity) CreateActivity(c *gin.Context) {

	var data *admin.Activity

	c.ShouldBindJSON(&data)

	claims, err := tools.GetClaims(c)

	if err != nil {
		utils.FailWithMessage("授权失败", c)
		return
	}

	err = services.ActivityService.CreateActivity(claims.BaseClaims, data)

	if err != nil {
		utils.FailWithMessage(err.Error(), c)
		return
	}

	utils.Ok(c)
}

func (*Activity) EditActivity(c *gin.Context) {

	var data *admin.Activity

	c.ShouldBindJSON(&data)

	claims, err := tools.GetClaims(c)

	if err != nil {
		utils.FailWithMessage("授权失败", c)
		return
	}

	err = services.ActivityService.EditActivity(claims.BaseClaims, data)

	if err != nil {
		utils.FailWithMessage(err.Error(), c)
		return
	}

	utils.Ok(c)
}

func (*Activity) GetActivityList(c *gin.Context) {
	id, _ := strconv.Atoi(c.DefaultQuery("louPanId", "0"))

	claims, err := tools.GetClaims(c)

	if err != nil {
		utils.FailWithMessage("授权失败", c)
		return
	}

	data, err := services.ActivityService.GetActivityList(claims.BaseClaims, id)

	if err != nil {
		utils.FailWithMessage(err.Error(), c)
		return
	}

	utils.OkWithData(data, c)
}

var ActivityController = new(Activity)
